<?php
namespace PsiFramework\Applications\DefaultApp\Classes\Model;

use PsiFramework\Classes\Util\Security\Model\SecurityRole;

import("Classes.Model.SessionUser");
import("Classes.Model.ModelInterface.IUser");
import("Applications.DefaultApp.Classes.Model.Gender");
import("Applications.DefaultApp.Classes.Model.City");
import("Classes.Util.Object.Date");

use \PsiFramework\Classes\Model\SessionUser;
use \PsiFramework\Classes\Model\ModelInterface\IUser;
use \PsiFramework\Applications\DefaultApp\Classes\Model\Gender;
use \PsiFramework\Applications\DefaultApp\Classes\Model\City;
use \PsiFramework\Classes\Util\Object\Date;

/**
 * PsiFramework
 * Php Simple Framework
 *
 * @author Rémi San
 * @version beta
 */

/**
 * The User class describes the information for a user
 *
 * @package Model
 */
class User extends SessionUser implements IUser
{

    protected /*Gender*/             $_gender;
    protected /*string*/             $_email;
    protected /*string*/             $_firstName;
    protected /*string*/             $_surName;
    protected /*string*/             $_picture;
    protected /*Date*/               $_birth;
    protected /*City*/               $_city;
    protected /*string*/             $_text;
    protected /*string*/             $_address;
    protected /*string*/             $_zipCode;
    protected /*string*/             $_cityName;
    protected /*Date*/               $_creationDate ;
    protected /*boolean*/            $_activated ;
    protected /*boolean*/            $_unsubscribed ;
    protected /*boolean*/            $_blocked ;
    protected /*boolean*/            $_tel ;
    protected /*boolean*/            $_tel2 ;
    protected /*boolean*/            $_fax ;
    protected /*SecurityUserRole[]*/ $_roles ;

    /**
     * User constructor
     *
     * @param int                $id           the id (unique) of the user
     * @param string             $login        the login of the user
     * @param string             $password     the password of the user
     * @param string             $email        the email of the user
     * @param Gender             $gender       the gender of the user
     * @param string             $firstName    the first name of the user
     * @param string             $surName      the surname of the user
     * @param string             $picture      the picture of the user
     * @param Date               $birth        the birth of the user
     * @param City               $city         the city of the user
     * @param string             $address      the address of the user
     * @param string             $zipCode      the zip code of the user
     * @param string             $cityName     the city name of the user
     * @param string             $text         the text of the user
     * @param Date               $creationDate the creation date
     * @param bool               $activated    has the user been activated
     * @param bool               $unsubscribed has the user unsubscribed
     * @param bool               $blocked      has the user been blocked
     * @param SecurityUserRole[] $roles        the roles
     * 
     * @return User
     */
    public function __construct(
        /*int*/    $id=null,
        /*string*/ $login=null,
        /*string*/ $password=null,
        /*string*/ $email=null,
        /*Gender*/ $gender=null,
        /*string*/ $firstName=null,
        /*string*/ $surName=null,
        /*string*/ $picture=null,
        /*date*/   $birth=null,
        /*City*/   $city=null,
        /*string*/ $address=null,
        /*string*/ $zipCode=null,
        /*string*/ $cityName=null,
        /*string*/ $text=null,
        /*Date*/   $creationDate=null,
        /*boolean*/ $activated=false,
        /*boolean*/ $unsubscribed=false,
        /*boolean*/ $blocked=false,
       /*SecurityUserRole[]*/ $roles=null
    )
    {
        parent::__construct($id, $login, $password);
        $this->_email = $email ;
        $this->_gender = $gender ;
        $this->_firstName = $firstName ;
        $this->_surName = $surName ;
        $this->_picture = $picture ;
        $this->_birth = $birth ;
        $this->_city = $city ;
        
        $this->_address = $address ;
        $this->_zipCode = $zipCode ;
        $this->_cityName = $cityName ;
        
        $this->_text = $text ;

        if ($creationDate === null) {
            $creationDate = Date::now();
        }
        $this->_creationDate = $creationDate ;
        $this->_activated = $activated ;
        $this->_unsubscribed = $unsubscribed ;
        $this->_blocked = $blocked ;

        $this->_roles = $roles ;
    }

    /**
     * Getter for the member variable $email
     *
     * @return     string    the email of the user
     */
    public function getEmail()
    {
        return $this->_email ;
    }

    /**
     * Setter for the member variable $email
     *
     * @param     string    $email    the email
     */
    public function setEmail($email)
    {
        $this->_email = $email ;
    }

    /**
     * Getter for the member variable $gender
     *
     * @return     Gender    the gender of the user
     */
    public function getGender()
    {
        return $this->_gender ;
    }

    /**
     * Setter for the member variable $gender
     *
     * @param     Gender    $gender    the gender
     */
    public function setGender($gender)
    {
        $this->_gender = $gender ;
    }

    /**
     * Getter for the member variable $firstName
     *
     * @return     string    the firstName of the user
     */
    public function getFirstName()
    {
        return $this->_firstName ;
    }

    /**
     * Setter for the member variable $firstName
     *
     * @param     string    $firstName    the firstName
     */
    public function setFirstName($firstName)
    {
        $this->_firstName = $firstName ;
    }

    /**
     * Getter for the member variable $surName
     *
     * @return     string    the surName of the user
     */
    public function getSurName()
    {
        return $this->_surName ;
    }

    /**
     * Setter for the member variable $surName
     *
     * @param     string    $surName    the surName
     */
    public function setSurName($surName)
    {
        $this->_surName = $surName ;
    }

    /**
     * Getter for the name
     *
     * @return     string    the name of the user
     */
    public function getName()
    {
    	return $this->_firstName . ' ' . $this->_surName ;
    }
    
    /**
     * Getter for the member variable $picture
     *
     * @return     string    the picture of the user
     */
    public function getPicture()
    {
        return $this->_picture ;
    }

    /**
     * Setter for the member variable $picture
     *
     * @param     string    $picture    the picture
     */
    public function setPicture($picture)
    {
        $this->_picture = $picture ;
    }

    /**
     * Getter for the member variable $birth
     *
     * @return Date    the birth of the user
     */
    public function getBirth()
    {
        return $this->_birth ;
    }

    /**
     * Setter for the member variable $birth
     *
     * @param     Date    $birth    the birthdate
     */
    public function setBirth($birth)
    {
        $this->_birth = $birth ;
    }

    /**
     * Getter for the member variable $city
     *
     * @return City    the city of the user
     */
    public function getCity()
    {
        return $this->_city ;
    }

    /**
     * Setter for the member variable $city
     *
     * @param     City    $city    the city
     */
    public function setCity(/*City*/ $city)
    {
        $this->_city = $city ;
    }
    
    /**
     * Getter for the member variable $address
     *
     * @return string the $address of the user
     */
    public function getAddress()
    {
    	return $this->_address ;
    }
    
    /**
     * Setter for the member variable $address
     *
     * @param string $address the address
     * 
     * @return void
     */
    public function setAddress(/*string*/ $address)
    {
    	$this->_address = $address ;
    }
    
    /**
     * Getter for the member variable $zipCode
     *
     * @return string the $zipCode of the user
     */
    public function getZipCode()
    {
    	return $this->_zipCode ;
    }
    
    /**
     * Setter for the member variable $zipCode
     *
     * @param string $zipCode the zip code
     * 
     * @return void
     */
    public function setZipCode(/*string*/ $zipCode)
    {
    	$this->_zipCode = $zipCode ;
    }
    
    /**
     * Getter for the member variable $cityName
     *
     * @return string the $cityName of the user
     */
    public function getCityName()
    {
    	return $this->_cityName ;
    }
    
    /**
     * Setter for the member variable $cityName
     *
     * @param string $cityName the city name
     * 
     * @return void
     */
    public function setCityName(/*string*/ $cityName)
    {
    	$this->_cityName = $cityName ;
    }

    /**
     * Getter for the member variable $text
     *
     * @return string    the $text of the user
     */
    public function getText()
    {
        return $this->_text ;
    }

    /**
     * Setter for the member variable $text
     *
     * @param string    $text    the text
     */
    public function setText(/*string*/ $text)
    {
        $this->_text = $text ;
    }

    /**
     * Getter for the member variable $creationDate
     *
     * @return     Date    the creationDate of the user
     */
    public function getCreationDate()
    {
        return $this->_creationDate ;
    }

    /**
     * Setter for the member variable $creationDate
     *
     * @param Date    $creationDate    the creation date
     */
    public function setCreationDate(/*Date*/ $creationDate)
    {
        $this->_creationDate = $creationDate ;
    }

    /**
     * Getter for the member variable $activated
     *
     * @return bool    the activation of the user
     */
    public function isActivated()
    {
        return $this->_activated ;
    }

    /**
     * Setter for the member variable $activated
     *
     * @param bool    $activated    activation
     */
    public function setActivated(/*boolean*/ $activated)
    {
        $this->_activated = $activated ;
    }

    /**
     * Getter for the member variable $unsubscribed
     *
     * @return boolean    has the user unsubscribed
     */
    public function isUnsubscribed()
    {
        return $this->_unsubscribed ;
    }

    /**
     * Setter for the member variable $unsubscribed
     *
     * @param boolean    $unsubscribed    the unsubscription
     */
    public function setUnsubscribed(/*boolean*/ $unsubscribed)
    {
        $this->_unsubscribed = $unsubscribed ;
    }

    /**
     * Getter for the member variable $blocked
     *
     * @return boolean    has the user been blocked
     */
    public function isBlocked()
    {
        return $this->_blocked ;
    }

    /**
     * Setter for the member variable $blocked
     *
     * @param boolean    $blocked    has the user been blocked
     */
    public function setBlocked(/*boolean*/ $blocked)
    {
        $this->_blocked = $blocked ;
    }

    /**
     * Getter for the member variable $roles
     *
     * @return the $roles of the user
     */
    public function getRoles()
    {
        return $this->_roles ;
    }

    /**
     * Setter for the member variable $roles
     *
     * @param $roles    the roles
     */
    public function setRoles($roles)
    {
        $this->_roles = $roles ;
    }

    public function addRole($role)
    {
        if (is_null($this->_roles)) {
            $this->_roles = array();
        }
        $this->_roles[] = $role ;
    }
    
    public function hasRole(/*string*/ $roleName)
    {
        foreach ($this->_roles as $role) {
            if ($role->getRole()->getName() == $roleName) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * Getter for the member variable $tel
     *
     * @return string
     */
    public function getTel()
    {
    	return $this->_tel ;
    }
    
    /**
     * Setter for the member variable $tel
     *
     * @param string $tel the tel
     * 
     * @return void
     */
    public function setTel(/*string*/ $tel)
    {
    	$this->_tel = $tel ;
    }
    
    /**
     * Getter for the member variable $tel2
     *
     * @return string
     * 
     * @return void
     */
    public function getTel2()
    {
    	return $this->_tel2 ;
    }
    
    /**
     * Setter for the member variable $tel2
     *
     * @param string $tel2 the tel2
     */
    public function setTel2(/*string*/ $tel2)
    {
    	$this->_tel2 = $tel2 ;
    }
    
    /**
     * Getter for the member variable $fax
     *
     * @return string
     */
    public function getFax()
    {
    	return $this->_fax ;
    }
    
    /**
     * Setter for the member variable $fax
     *
     * @param string $fax the fax
     * 
     * @return void
     */
    public function setFax(/*string*/ $fax)
    {
    	$this->_fax = $fax ;
    }
    
    /**
     * 
     * @return NULL|string
     */
    public function getFormattedEmailString()
    {
        
        if (isNullOrEmptyString($this->_email)) {
            return null;
        }
        
        $name = '';
        if (isNullOrEmptyString($this->_firstName)) {
            $name .= $this->_firstName;
        }
        
        if (isNullOrEmptyString($this->_surName)) {
            if (!isNullOrEmptyString($name)) {
                $name .= ' ';
            }
            $name .= $this->_surName;
        }
        
        if (!isNullOrEmptyString($name)) {
            return '"'.$name.'" <'.$this->_email.'>';
        } else {
            return $this->_email;
        }
    }
    
    public function getCustomerAlias()
    {
        return 'PSI_'.$this->_id;
    }
    
}
